package cn.glutnn.service.impl;

import cn.glutnn.entity.AdminUser;
import cn.glutnn.mapper.AdminUserMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.authority.SimpleGrantedAuthority;
import org.springframework.security.core.userdetails.User;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.stereotype.Service;

import java.util.Collections;

/**
 * UserDetailsService实现类
 * @author msh
 * @date 2025/04/04
 */
@Service
public class UserDetailsServiceImpl implements UserDetailsService {
    
    @Autowired
    private AdminUserMapper adminUserMapper;
    
    @Override
    public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException {
        // 查询管理员用户
        AdminUser adminUser = adminUserMapper.findByUsername(username);
        
        if (adminUser == null) {
            throw new UsernameNotFoundException("用户不存在");
        }
        
        // 创建UserDetails对象
        return new User(
                adminUser.getUsername(),
                adminUser.getPassword(),
                Collections.singletonList(new SimpleGrantedAuthority("ROLE_" + adminUser.getRole()))
        );
    }
} 